/**
 * 项目数据库集合架构定义
 * 包含所有集合的结构和验证规则
 */

const { PROJECT_SCHEMA } = require('./miniprogram/constants/project_schema.js');

// 项目集合架构
const schemaProps = {};
Object.keys(PROJECT_SCHEMA).forEach(key => {
  // 解析字段定义
  const [type, required, ...rest] = PROJECT_SCHEMA[key].split('|');
  const fieldProps = { bsonType: type === 'int' ? 'int' : 'string' };
  
  // 处理其他属性
  rest.forEach(prop => {
    if (prop.startsWith('default=')) {
      const defaultValue = prop.split('=')[1];
      fieldProps.default = type === 'int' ? parseInt(defaultValue) : defaultValue;
    }
    if (prop.startsWith('comment=')) {
      fieldProps.description = prop.split('=')[1];
    }
  });

  // 如果是必填字段，添加相应验证
  if (required === 'true') {
    fieldProps.minLength = type === 'string' ? 1 : undefined;
    fieldProps.minimum = type === 'int' ? 0 : undefined;
  }

  schemaProps[key] = fieldProps;
});

const PROJECT_COLLECTION_SCHEMA = {
  validator: {
    $jsonSchema: {
      bsonType: 'object',
      required: Object.keys(PROJECT_SCHEMA).filter(k => PROJECT_SCHEMA[k].includes('|true')), // 必填字段
      properties: schemaProps
    }
  }
};

// 项目分支集合架构
const PROJECT_BRANCH_SCHEMA = {
  validator: {
    $jsonSchema: {
      bsonType: 'object',
      required: ['projectId', 'name', 'createdTime'],
      properties: {
        _id: { bsonType: 'string' },
        projectId: { bsonType: 'string' },
        name: { bsonType: 'string' },
        parentId: { bsonType: 'string' },
        createdTime: { bsonType: 'int' },
        createdBy: { bsonType: 'string' },
        
        // 源节点信息（记录从哪个具体节点创建的分支）
        sourceNodeId: { bsonType: 'string' },
        sourceNodeTime: { bsonType: 'int' },
        sourceNodeStatus: {
          bsonType: 'object',
          properties: {
            level1: { bsonType: 'int' },
            level2: { bsonType: 'string' },
            level3: { bsonType: 'string' },
            level4: { bsonType: 'string' }
          }
        },
        
        // 当前状态
        status: {
          bsonType: 'object',
          required: ['level1', 'level2'],
          properties: {
            level1: { bsonType: 'int' },
            level2: { bsonType: 'string' },
            level3: { bsonType: 'string' },
            level4: { bsonType: 'string' }
          }
        },
        
        completionPercentage: { bsonType: 'int' },
        isMaster: { bsonType: 'bool' },
        isActive: { bsonType: 'bool' },
        
        // 系统字段
        _pid: { bsonType: 'string' },
        _createTime: { bsonType: 'int' },
        _updateTime: { bsonType: 'int' },
        _createBy: { bsonType: 'string' },
        _updateBy: { bsonType: 'string' }
      }
    }
  }
};

// 项目状态历史集合架构
const PROJECT_STATE_HISTORY_SCHEMA = {
  validator: {
    $jsonSchema: {
      bsonType: 'object',
      required: ['projectId', 'branchId', 'fromState', 'toState', 'createdTime'],
      properties: {
        _id: { bsonType: 'string' },
        projectId: { bsonType: 'string' },
        branchId: { bsonType: 'string' },
        
        // 状态变更
        fromState: {
          bsonType: 'object',
          properties: {
            level1: { bsonType: 'int' },
            level2: { bsonType: 'string' },
            level3: { bsonType: 'string' },
            level4: { bsonType: 'string' }
          }
        },
        toState: {
          bsonType: 'object',
          properties: {
            level1: { bsonType: 'int' },
            level2: { bsonType: 'string' },
            level3: { bsonType: 'string' },
            level4: { bsonType: 'string' }
          }
        },
        
        eventType: { bsonType: 'string' },
        comment: { bsonType: 'string' },
        createdBy: { bsonType: 'string' },
        createdTime: { bsonType: 'int' },
        
        // 系统字段
        _pid: { bsonType: 'string' },
        _createTime: { bsonType: 'int' },
        _updateTime: { bsonType: 'int' }
      }
    }
  }
};

// 导出集合架构
module.exports = {
  PROJECT_COLLECTION_SCHEMA,
  PROJECT_BRANCH_SCHEMA,
  PROJECT_STATE_HISTORY_SCHEMA
}; 